$(document).ready(function() {
	//获取项目名
	var contextPath = document.location.pathname; 
	var index =contextPath.substr(1).indexOf("/"); 
	contextPath = contextPath.substr(0,index+1); 
	
	var totalCount = $("#totalCount").val();
	var pSize = $("#pageSize").val();
	var pageIndex = $("#pageIndex").val();
	var pageCount = $("#pageCount").val();
	var gdeArr = new Array()// 定义一个数组,放礼品Id
	
	/**
	 * 匿名评论的复选框的值的处理
	 * */
	$(".divLeft").delegate('#anonymous', 'click', function() {
		if($("#anonymous").attr('checked')=='checked'){
			$("#anonymous").removeAttr('checked');
			$("#anonymous").val('0');
		}else{
			$("#anonymous").attr('checked','checked');
			$("#anonymous").val('1')
		}
	});
	
	/**
	 * 清空数组
	 */
	function clearArrAll(){
		if(gdeArr.length>0){
			gdeArr.splice(0,gdeArr.length);
		}
		//获取gdeId的值
		var numgdeId = $("#num").val();
		
		for(i=0;i<numgdeId;i++){
			//礼品规格Id
			var gdeId = $("#gdeId_"+i).val();
			gdeArr.push(gdeId);
		}
	}
	
	
//分页
	
	$('#pagination2').jqPaginator({
		totalCounts: totalCount,
		pageSize: pSize,
	    visiblePages: pageCount,
	    currentPage: 1,
	    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
	    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
	    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
		onPageChange : function(num, type) {
			clearArrAll();//清空数组再加载数据
			
			$("#pageIndex").val(num);
			var queryUrl = contextPath+"/HRTWEB/commentPage/findCommentByGdeId.do";
			$.ajax({
				url : queryUrl,
				dataType : 'json',
				data : {
					pageIndex :num,
					pageSize : pSize,
					ids:gdeArr.toString()
				},
				cache : false,
				success : function(data) {
					var results = data.pageResult.resultList;
					//礼品基本信息
					$("#commentList tr").remove();
					for (i = 0; i < results.length; i++) {
						var commAll = results[i];
						//替换匿名的用户名
						var mbr = commAll.hrtMemberName;
						if(mbr!=null){
							if(commAll.anonymous==1){
								var subUserName = mbr.substring(1,mbr.length-1);
								commAll.hrtMemberName = mbr.replace(subUserName,"***");
							}
						}else{
							commAll.hrtMemberName = "";
						}

						var rowContent = "<tr > " 
							+ "<td>初次评论: <div style='line-height: 10px; font-size: 12px;'>5-05</div> " 
							+ "</td> " 
							+ "<td>"
							+ commAll.id
							+ commAll.content
							+ "<div style='width: 20px; line-height: 20px; text-align: cneter;'> " 
							+ "<img src='<%=request.getContextPath()%>/HRTWEB/images/stars-all.gif' /> </div> " 
							+ "</td> " 
							+ "<td>" 
							+ commAll.hrtMemberName
							+ "(";
						
							if(commAll.anonymous==1){
								rowContent = rowContent + "匿名" ;
							}else{
								rowContent = rowContent + "公开" ;
							}
							
							rowContent = rowContent + ")</td> "  
							+ "</tr>";
						$("#commentList") .append(rowContent);
						
					}
				},
				error : function(html) {
					alert("gdeId Data failly");
					return;
				}
			});
		}
	});
	
	
	
	//单选按钮
	$("#commAllAdd").delegate('[name="chiceComment"]', 'click', function() {
		clearArrAll();//清空数组再加载数据
		var commNum= $(this).val();
		if(commNum=="1"){
			$('#pagination2').jqPaginator({
				totalCounts: totalCount,
				pageSize: pSize,
			    visiblePages: pageCount,
			    currentPage: 1,
			    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
			    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
			    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
				onPageChange : function(num, type) {
					clearArrAll();//清空数组再加载数据
					
					$("#pageIndex").val(num);
					var queryUrl = contextPath+"/HRTWEB/commentPage/findCommentByGdeId.do";
					$.ajax({
						url : queryUrl,
						dataType : 'json',
						data : {
							pageIndex :num,
							pageSize : pSize,
							ids:gdeArr.toString()
						},
						cache : false,
						success : function(data) {
							var results = data.pageResult.resultList;
							//礼品基本信息
							$("#commentList tr").remove();
							for (i = 0; i < results.length; i++) {
								var commAll = results[i];
								
								//替换匿名的用户名
								var mbr = commAll.hrtMemberName;
								if(mbr!=null){
									if(commAll.anonymous==1){
										var subUserName = mbr.substring(1,mbr.length-1);
										commAll.hrtMemberName = mbr.replace(subUserName,"***");
									}
								}else{
									commAll.hrtMemberName = "";
								}
								
								var rowContent = "<tr > " 
									+ "<td>初次评论: <div style='line-height: 10px; font-size: 12px;'>5-05</div> " 
									+ "</td> " 
									+ "<td>"
									+ commAll.id
									+ commAll.content
									+ "<div style='width: 20px; line-height: 20px; text-align: cneter;'> " 
									+ "<img src='<%=request.getContextPath()%>/HRTWEB/images/stars-all.gif' /> </div> " 
									+ "</td> " 
									+ "<td>" 
									+ commAll.hrtMemberName
									+ "(";
								
									if(commAll.anonymous==1){
										rowContent = rowContent + "匿名" ;
									}else{
										rowContent = rowContent + "公开" ;
									}
									
									rowContent = rowContent + ")</td> "  
									+ "</tr>";
								$("#commentList") .append(rowContent);
								
							}
						},
						error : function(html) {
							alert("gdeId Data failly");
							return;
						}
					});
				}
			});
		}
		if(commNum=="2"){
			var queryUrl = contextPath+"/HRTWEB/commentPage/getAddCommentByGdeId.do";
			$.ajax({
				url : queryUrl,
				dataType : 'json',
				data : {
					pageIndex :1,
					pageSize : 10,
					ids:gdeArr.toString()
				},
				cache : false,
				success : function(data) {
					//初始化分页各值
					totalCount = data.pageResult.totalCount;
					pSize=data.pageResult.pageSize;
					pageCount=data.pageResult.pageCount;
//					alert("a=totalCount==>"+totalCount+"==pSize==b----->"+pSize+"===pageCount=c--->"+pageCount);
					
					$('#pagination2').jqPaginator({
						totalCounts: totalCount,
						pageSize: pSize,
					    visiblePages: pageCount,
					    currentPage: 1,
					    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
					    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
					    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
						onPageChange : function(num, type) {
							clearArrAll();//清空数组再加载数据
							
							$("#pageIndex").val(num);
							var queryUrl = contextPath+"/HRTWEB/commentPage/getAddCommentByGdeId.do";
							$.ajax({
								url : queryUrl,
								dataType : 'json',
								data : {
									pageIndex :num,
									pageSize : pSize,
									ids:gdeArr.toString()
								},
								cache : false,
								success : function(data) {
									var results = data.pageResult.resultList;
									//礼品基本信息
									$("#commentList tr").remove();
									for (i = 0; i < results.length; i++) {
										var commAll = results[i];
										//替换匿名的用户名
										var mbr = commAll.hrtMemberName;
										if(mbr!=null){
											if(commAll.anonymous==1){
												var subUserName = mbr.substring(1,mbr.length-1);
												commAll.hrtMemberName = mbr.replace(subUserName,"***");
											}
										}else{
											commAll.hrtMemberName = "";
										}
										var rowContent = "<tr > " 
											+ "<td>追评评论: <div style='line-height: 10px; font-size: 12px;'>5-05</div> " 
											+ "</td> " 
											+ "<td>"
											+ commAll.id
											+ commAll.content
											+ "<div style='width: 20px; line-height: 20px; text-align: cneter;'> " 
											+ "<img src='<%=request.getContextPath()%>/HRTWEB/images/stars-all.gif' /> </div> " 
											+ "</td> " 
											+ "<td>" 
											+ commAll.hrtMemberName
											+ "(";
										
											if(commAll.anonymous==1){
												rowContent = rowContent + "匿名" ;
											}else{
												rowContent = rowContent + "公开" ;
											}
											
											rowContent = rowContent + ")</td> "  
											+ "</tr>";
										$("#commentList") .append(rowContent);
										
									}
								},
								error : function(html) {
									alert("gdeId Data failly");
									return;
								}
							});
						}
					});
				},
				error : function(html) {
					alert("gdeId Data failly");
					return;
				}
			});
			
		}
			
	});
	
	
	
	/**
	 * 添加
	 * */
	$("#commBtn").delegate('#submitBtn', 'click', function() {
		var num = $("#num").val();
		for(i=0;i<num;i++){
			var contentDet = $("#contentDetAll_"+i+" div").html();
			$("#contentDet_"+i).val(contentDet);
			var logistics = $("#logisticsAll div").html();
			$("#logistics_"+i).val(logistics);
			var servicerComm = $("#servicerCommAll div").html();
			$("#servicerComm_"+i).val(servicerComm);
			//匿名评论
			var anonymous = $("#anonymous").val();
			$("#anonymous_"+i).val(anonymous);
		}
		$("#comments").attr("method","post");
		$("#comments").attr("action",contextPath+"/HRTWEB/commentPage/saveWeb.do");
		
	});
	
	
	
});

